package cn.offcn.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        System.out.println("进入loginFilter5555555555555555555555555555555555555555555555555");

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String uri = request.getRequestURI();



        boolean isFlag = uri.endsWith(".jsp");
        System.out.println("进入loginFilter");
        if (isFlag) {
            String page = uri.substring(uri.lastIndexOf("/") + 1);
            if (page.equals("login.jsp")) {
                chain.doFilter(request, response);
                return;
            } else {
                Object obj = request.getSession().getAttribute("activeEmployee");
                if (obj == null) {
                    response.sendRedirect(request.getContextPath() + "/pages/login.jsp");
                    return;
                }
            }
        }
        chain.doFilter(request, response);
    }
}
